﻿using DelonixRegia.Classes;
using DelonixRegia.Classes.Reporting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;

namespace DelonixRegia
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        static string room = "";
        static ArrayList roomlist = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            // CHEW SENG'S CODE - DON'T TOUCH //
            Account a = (Account)Session["acc"];

            if (a != null)
            {
                if (a.userType < 2)
                    Response.Redirect("Default.aspx");
            }
            else
                Response.Redirect("SignIn.aspx");
            // CHEW SENG'S CODE - DON'T TOUCH //

            //ArrayList ol = new ArrayList();
            //gvOccupancy.DataSource = ol;
            //gvOccupancy.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if(ddlRoom.SelectedValue != "Not Selected"){
                roomlist = new ArrayList();
                room = ddlRoom.SelectedItem.Text;
                
                roomlist = reportingDBManager.getBookingDetailsByRoom(Convert.ToInt32(ddlRoom.SelectedItem.Text));
                gvOccupancy.DataSource = roomlist;
                gvOccupancy.DataBind();

                pnlFirstStep.Visible = false;
                pnlSecondStep.Visible = true;
                Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
            }
            else
            {
                ClientScriptMgr.showErrorBox("No occupied rooms in this floor");
            }

        }

        protected void ddlFloor_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlFloor.SelectedItem.Text == "Not Selected")
            {
                ddlRoom.Enabled = false;
            }
            else
            {
                ArrayList roomlist = reportingDBManager.getAllOccupiedRooms(Convert.ToInt32(ddlFloor.SelectedItem.Text));
                if (roomlist.Count == 0)
                {
                    roomlist.Add("Not Selected");
                }
                ddlRoom.DataSource = roomlist;
                ddlRoom.DataBind();
                ddlRoom.Enabled = true;
            }
        }

        protected void ExcelBut_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Charset = "";
            string FileName = "Guests in room: " + room + " " + DateTime.Now.ToString() + ".xls";
            StringWriter strwritter = new StringWriter();
            HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);

            gvOccupancy.GridLines = GridLines.Both;
            gvOccupancy.HeaderStyle.Font.Bold = true;
            gvOccupancy.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());

            Response.End();

        }

        protected void PDFBut_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.pdf", "Guests in room: " + room + " " + DateTime.Now.ToString()));
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            System.IO.StringWriter stringWrite1 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1);
            DataGrid myDataGrid1 = new DataGrid();
            myDataGrid1.DataSource = roomlist;
            myDataGrid1.DataBind();
            myDataGrid1.RenderControl(htmlWrite1);


            StringReader sr = new StringReader("Guests in room: " + stringWrite1.ToString());
            Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            pdfDoc.SetPageSize(PageSize.A4.Rotate());
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            HttpContext.Current.Response.Write(pdfDoc);
            HttpContext.Current.Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
               server control at run time. */
        }

        protected void PrintBut_Click(object sender, EventArgs e)
        {
            //gvOccupancy.AllowPaging = false;
            //gvOccupancy.DataSource = roomlist;
            //gvOccupancy.DataBind();
            //StringWriter sw = new StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(sw);
            //gvOccupancy.RenderControl(hw);
            //string gridHTML = sw.ToString().Replace("\"", "'")
            //    .Replace(System.Environment.NewLine, "");
            //StringBuilder sb = new StringBuilder();
            //sb.Append("<script type = 'text/javascript'>");
            //sb.Append("window.onload = new function(){");
            //sb.Append("var printWin = window.open('', '', 'left=0");
            //sb.Append(",top=0,width=1000,height=600,status=0');");
            //sb.Append("printWin.document.write(\"");
            //sb.Append(gridHTML);
            //sb.Append("\");");
            //sb.Append("printWin.document.close();");
            //sb.Append("printWin.focus();");
            //sb.Append("printWin.print();");
            //sb.Append("printWin.close();};");
            //sb.Append("</script>");
            //ClientScript.RegisterStartupScript(this.GetType(), "Room Status Report", sb.ToString());
            //gvOccupancy.AllowPaging = true;
            //gvOccupancy.DataSource = roomlist;
            //gvOccupancy.DataBind();
        }  
    }
}